//
//  LZLinkOrEditEmailViewController.m
//  laziz_Merchant
//
//  Created by xbnzd on 17/3/20.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZLinkOrEditEmailViewController.h"
#import "UIViewController+NavigationItemSetting.h"
#import "GVUserDefaults+LZProperties.h"
#import "extobjc.h"

#import "LZUserInfoPresenter.h"
#import "LZRemindView.h"

@interface LZLinkOrEditEmailViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UILabel *topViewTitleLabel;
@property (weak, nonatomic) IBOutlet UITextField *topViewTF;
@property (weak, nonatomic) IBOutlet UILabel *secondViewTitleLabel;
@property (weak, nonatomic) IBOutlet UITextField *secondViewTF;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *secondViewTopToSuperViewCST;

@property (strong, nonatomic) UIButton *saveBtn;
@property (strong, nonatomic) LZUserInfoPresenter *userInfoPresenter;

@end

@implementation LZLinkOrEditEmailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configureUI];
    
    [self configureNavBar];
}

#pragma mark - private
/**
 每次编辑操作, 检查按钮状态
 */
- (void)chnageSaveState
{
    self.saveBtn.userInteractionEnabled = NO;
    [self.saveBtn setTitleColor:UIColorFromRGB(0x666666, 1) forState:UIControlStateNormal];
    if (self.isLinkEmail) {
        if (self.secondViewTF.text.length < 6) {
            return;
        }
    }else{
        if (self.topViewTF.text.length < 6 || self.secondViewTF.text.length < 6) {
            return;
        }
    }
    
    [self.saveBtn setTitleColor:UIColorFromRGB(0xffffff, 1) forState:UIControlStateNormal];
    self.saveBtn.userInteractionEnabled = YES;
    
}


/**
 保存按钮
 */
- (void)saveAction
{
    if (self.isLinkEmail) {
        [self bindEmailAction];
    }else{
        [self changeEmailAction];
    }
}


/**
 绑定邮箱
 */
- (void)bindEmailAction
{
    @weakify(self);
   [self.userInfoPresenter bindEmailWithEmail:self.secondViewTF.text Success:^(id data) {
       @strongify(self);
       [GVUserDefaults standardUserDefaults].email = self.secondViewTF.text;
       [self.navigationController popViewControllerAnimated:YES];
   } Failure:^(NSString *fail) {
       if ([fail isEqualToString:@"0500017"]) {//绑定Email失败
           [LZRemindView initWithImage:[UIImage imageNamed:@"错误提示"] andContent:LZLocalizedString(@"", @"")];
       }else if ([fail isEqualToString:@"0400044"]){//邮箱已经被绑定
           [LZRemindView initWithImage:[UIImage imageNamed:@"错误提示"] andContent:LZLocalizedString(@"person_bind_email_is_have", @"")];
       }
   }];
}


 
/**
 修改邮箱
 */
- (void)changeEmailAction
{
    @weakify(self);
    [self.userInfoPresenter changeEmailWithOldEmail:self.topViewTF.text NewEmail:self.secondViewTF.text Success:^(id data) {
        @strongify(self);
        NSDictionary *dic = (NSDictionary *)data;
         [GVUserDefaults standardUserDefaults].userToken = dic[@"data"][@"token"];
        [GVUserDefaults standardUserDefaults].email = dic[@"data"][@"email"];
        
        [self.navigationController popViewControllerAnimated:YES];
        
    } Failure:^(NSString *fail) {
        if ([fail isEqualToString:@"0500018"]) {//修改Email失败
            [LZRemindView initWithImage:[UIImage imageNamed:@"错误提示"] andContent:LZLocalizedString(@"", @"")];
        }
    }];
}

#pragma mark - TF delegate
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self chnageSaveState];
}

#pragma mark - configure UI
- (void)configureUI
{
    self.umLogPageViewName = @"绑定或者修改邮箱";
    _userInfoPresenter = [[LZUserInfoPresenter alloc] init];
    self.view.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
    
    if (self.isLinkEmail) {
        self.title = LZLocalizedString(@"person_set_email", nil);
        self.topView.hidden = YES;
        self.secondViewTopToSuperViewCST.constant = 0;
        
        self.secondViewTitleLabel.text = LZLocalizedString(@"person_new_email", nil);
        
        self.secondViewTF.delegate = self;
        self.secondViewTF.placeholder = LZLocalizedString(@"person_enter_new_email", nil);
        
    }else{
        
        self.title = LZLocalizedString(@"person_change_email", nil);
        self.topViewTitleLabel.text = LZLocalizedString(@"person_old_email", nil);
        
        self.topViewTF.delegate = self;
        self.topViewTF.placeholder = LZLocalizedString(@"person_enter_old_email", nil);
        self.topViewTF.text = [GVUserDefaults standardUserDefaults].email;
        
        self.secondViewTitleLabel.text = LZLocalizedString(@"person_new_email", nil);
        
        self.secondViewTF.delegate = self;
        self.secondViewTF.placeholder = LZLocalizedString(@"person_enter_new_email", nil);
    }
    
}

- (void)configureNavBar
{
    self.saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.saveBtn.frame = CGRectMake(0, 0, 40, 40);
    self.saveBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    self.saveBtn.userInteractionEnabled = NO;
    [self.saveBtn setTitleColor:UIColorFromRGB(0x666666, 1) forState:UIControlStateNormal];
    [self.saveBtn setTitle:LZLocalizedString(@"save", nil) forState:UIControlStateNormal];
    [self.saveBtn addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
    [self navigationItemSetting:@[self.saveBtn] type:NAVIGATIONITEMSETTING_RIGHT];
}

@end
